/**
 * 
 */
package com.ekingstar.framework.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.app.ApplicationParameter;
import com.ekingstar.framework.domain.Function;
import com.ekingstar.framework.domain.Role;
import com.ekingstar.framework.service.IFunctionService;
import com.ekingstar.framework.service.IRoleService;
import com.ekingstar.framework.util.Page;

/**
 * 
 *
 */
@Action(name="list",namespace="/function")
public class ListFunctionAction extends ActionSupport implements ServletResponseAware{

	private HttpServletResponse response;
	private IFunctionService functionService;
	private IRoleService roleService;
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	
	public void setFunctionService(IFunctionService functionService) {
		this.functionService = functionService;
	}
	private Function function;
	private Role role;
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	private Page page;
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	private int pageNo = 1;
	private int pageSize = ApplicationParameter.INSTANCE.getPageSize();
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public Function getFunction() {
		return function;
	}
	public void setFunction(Function function) {
		this.function = function;
	}
	
	public String getChildren(){
		try{
		List res = functionService.getChildren(function);
		HttpServletResponse resp =  ServletActionContext.getResponse();
		resp.setContentType("text/paint; charset=UTF-8");
		
		final JsonConfig jc = new JsonConfig();
		jc.setJavaPropertyFilter(new PropertyFilter(){
			public boolean apply(Object source, String name, Object value) {
				// TODO Auto-generated method stub
				if ("parent".equals(name)) return true;
				return false;
			}
			
		});
		JSONArray funcs = JSONArray.fromObject(res,jc);
		String json = funcs.toString();
		System.out.println(json);
		resp.getWriter().print(json);
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	public String getFS(){
		try{
		List fs = functionService.getFunctionStructure(true);
		List menus = new ArrayList<Menu>();
	    if (fs != null && !fs.isEmpty())   {
	    	for ( int  i = 0; i < fs.size();i++){
	    		Function function  =(Function)fs.get(i);
	    		Menu menu = new Menu();
	    		menu.setText(function.getName());
	    		if (function.getLeaf() == null || !function.getLeaf().booleanValue() ){
					menu.setLeaf(false);
					menu.setCls("folder");
				}
				else {
					
					menu.setLeaf(true);
					menu.setCls("Benz");
				}
	    		
	    		menu.setId(function.getId());
	    		//menu.setChecked(true);
	    		menus.add(menu);
	    		generateMenu(menu,function);
	    	}
	    }
        JSONArray jsonObject = JSONArray.fromObject(menus);
        response.setContentType("text/paint; charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	
	public String getFSWithCheck(){
		try{
		List fs = functionService.getFunctionStructure(true);
		List functions = roleService.getFunctionsByRoleId(role.getId());
		List menus = new ArrayList<Menu>();
	    if (fs != null && !fs.isEmpty())   {
	    	for ( int  i = 0; i < fs.size();i++){
	    		Function function  =(Function)fs.get(i);
	    		CheckedMenu menu = new CheckedMenu();
	    		menu.setText(function.getName());
	    		menu.setCls("folder");
	    		menu.setLeaf(false);
	    		menu.setId(function.getId());
	    		//menu.setChecked(true);
	    		menus.add(menu);
	    		generateCheckMenu(menu,function,functions);
	    		this.checkMenu(menu, functions);
	    	}
	    }
        JSONArray jsonObject = JSONArray.fromObject(menus);
        response.setContentType("text/paint; charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		}catch(Throwable e){
			e.printStackTrace();
		}
		return null;
	}
	private void generateMenu(Menu menu,Function fun){
		if (menu == null || fun == null) return;
		menu.setText(fun.getName());
		menu.setId(fun.getId());
		List childen = fun.getChilden();
		if (fun.getLeaf() == null || !fun.getLeaf().booleanValue() ){
			menu.setLeaf(false);
			menu.setCls("folder");
		}
		else {
			menu.setLeaf(true);
			menu.setCls("Benz");
		}
		if (childen != null  && childen.size() > 0){
			List menChilden = new ArrayList();
			menu.setChildren(menChilden);
			for (int i = 0;i < childen.size();i++){
				Function cfun = (Function)childen.get(i);
				Menu cm = new Menu();
				generateMenu(cm,cfun);
				menChilden.add(cm);
			}
		}
	}
	private void generateCheckMenu(CheckedMenu menu,Function fun,List functions){
		if (menu == null || fun == null) return;
		menu.setText(fun.getName());
		menu.setId(fun.getId());
		List childen = fun.getChilden();
		if (fun.getLeaf() == null || !fun.getLeaf().booleanValue() ){
			menu.setLeaf(false);
			menu.setCls("folder");
		}
		else {
			menu.setLeaf(true);
			menu.setCls("Benz");
		}
		if (childen != null  && childen.size() > 0){
			List menChilden = new ArrayList();
			menu.setChildren(menChilden);
			for (int i = 0;i < childen.size();i++){
				Function cfun = (Function)childen.get(i);
				CheckedMenu cm = new CheckedMenu();
				generateCheckMenu(cm,cfun,functions);
				this.checkMenu(cm, functions);
				menChilden.add(cm);
			}
		}
	}
	public String deleteFunction(){
		if (function == null || function.getId() == null || function.getId().trim().equals("")) return "list";
		functionService.deleteFunction(function.getId());
		page = functionService.queryFunctions(function, pageNo, pageSize);	
		return "list";
	}
	public String execute(){
		page = functionService.queryFunctions(function, pageNo, pageSize);	
		return "list";
	}
	
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response = response;
	}
	
	//
	private void checkMenu(CheckedMenu menu,List functions){
		if (menu == null || functions == null || (functions.size() <= 0)) return;
		for (int i = 0;i  < functions.size() ; i++){
			Function func = (Function)functions.get(i);
			if (menu.getId().equals(func.getId())){
				menu.setChecked(true);
			}
		}
	}
}
